package com.example.hasee.zhcd.Activities.ShangWu;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hasee.zhcd.Activities.ChuanWu.ArrivalsDetailActivity;
import com.example.hasee.zhcd.Activities.ChuanWu.PictrueActivity;
import com.example.hasee.zhcd.Base.BaseActivity;
import com.example.hasee.zhcd.Bean.EventMsg;
import com.example.hasee.zhcd.R;
import com.example.hasee.zhcd.Util.GlobalParameter;
import com.example.hasee.zhcd.Util.ProgressDialogUtils;

import org.json.JSONObject;

import java.util.HashMap;

import static com.example.hasee.zhcd.R.id.picture;
import static com.example.hasee.zhcd.R.id.scrollView;

public class AtPortDetailActivity extends BaseActivity {
    private TextView title;
    private ImageView back;
    private ImageView home;
    private String strSYN;
    private String bmpString;
    private TextView fshipname;
    private TextView fhc;
    private TextView fberth;
    private TextView fwtf;
    private TextView fybton;
    private TextView fpzton;
    private TextView fzx;
    private TextView fhl;
    private TextView flx;
    private TextView fkbsj;
    private TextView fkgsj;
    private TextView fwgsj;
    private TextView flgsj;
    private TextView fwxp;
    private TextView fyb;
    private TextView fzd;
    private TextView fht;
    private ImageView fscandoc;
    private Bitmap bitmap;

    @Override
    public int contentViewId() {
        return R.layout.activity_at_port_detail;
    }

    @Override
    public void set() {
        title.setText("在港船舶详情");
        back.setOnClickListener(this);
        home.setVisibility(View.INVISIBLE);
        fscandoc.setOnClickListener(this);
        getDetail();
    }

    @Override
    public void init() {
        strSYN = getIntent().getExtras().getString("strSYN");
        title = (TextView) findViewById(R.id.centertitle);
        back = (ImageView) findViewById(R.id.back);
        home = (ImageView) findViewById(R.id.home);
        fshipname = (TextView) findViewById(R.id.fshipname);
        fhc = (TextView) findViewById(R.id.fhc);
        fberth = (TextView) findViewById(R.id.fberth);
        fwtf = (TextView) findViewById(R.id.fwtf);
        fybton = (TextView) findViewById(R.id.fybton);
        fpzton = (TextView) findViewById(R.id.fpzton);
        fzx = (TextView) findViewById(R.id.fzx);
        fhl = (TextView) findViewById(R.id.fhl);
        flx = (TextView) findViewById(R.id.flx);
        fkbsj = (TextView) findViewById(R.id.fkbsj);
        fkgsj = (TextView) findViewById(R.id.fkgsj);
        fwgsj = (TextView) findViewById(R.id.fwgsj);
        flgsj = (TextView) findViewById(R.id.flgsj);
        fwxp = (TextView) findViewById(R.id.fwxp);
        fyb = (TextView) findViewById(R.id.fyb);
        fzd = (TextView) findViewById(R.id.fzd);
        fht = (TextView) findViewById(R.id.fht);
        fscandoc = (ImageView) findViewById(picture);
    }

    @Override
    public void getEventData(EventMsg msg) {
        switch (msg.getAction()) {
            case "Fail":
                ProgressDialogUtils.closeProgressDialog();
                Toast.makeText(AtPortDetailActivity.this, "操作失败，请检查网络！", Toast.LENGTH_LONG).show();
                break;
            case "ZHSW_zgcbDetail":
                Log.e("ZHSW_zgcbDetail", msg.getMsg());
                ProgressDialogUtils.closeProgressDialog();
//E/ZHSW_zgcbDetail: {"data":[{"fshipname":"长昌南海", "fhc":"1803", "fberth":"", "fwtf":"浙江长昌海运有限公司",
// "fybton":"", "fhl":"煤炭", "fwxp":"", "fzd":"是", "fht":"", "fkbsj":"", "fkgsj":"", "fwgsj":"", "flgsj":"",
// "fpzton":"57034", "fyb":"", "flx":"", "fshipyearnumvcr":"QHD201802454", "fscandoc":
                try {
                    JSONObject json = new JSONObject(msg.getMsg());
                    String rspcode = json.getString("rspcode");
                    String rspmessage = json.getString("rspmessage");
                    if ("0".equals(rspcode)) {
                        JSONObject data = json.getJSONArray("data").getJSONObject(0);
                        fshipname.setText(data.getString("fshipname"));
                        fhc.setText(data.getString("fhc"));
                        fberth.setText(data.getString("fberth"));
                        fwtf.setText(data.getString("fwtf"));
                        fybton.setText(data.getString("fybton"));
                        fpzton.setText(data.getString("fpzton"));
                        fzx.setText(data.getString("flgmd"));
                        fhl.setText(data.getString("fhl"));
                        flx.setText(data.getString("flx"));
                        fkbsj.setText(data.getString("fkbsj"));
                        fkgsj.setText(data.getString("fkgsj"));
                        fwgsj.setText(data.getString("fwgsj"));
                        flgsj.setText(data.getString("flgsj"));
                        fwxp.setText(data.getString("fwxp"));
                        fyb.setText(data.getString("fyb"));
                        fzd.setText(data.getString("fzd"));
                        fht.setText(data.getString("fht"));
                        if ("".equals(data.getString("fscandoc"))) {
                            fscandoc.setClickable(false);
                        } else {
                            bmpString = data.getString("fscandoc");
                            bitmap = base64ToBitmap(bmpString);
                            fscandoc.setImageBitmap(bitmap);
                        }
                    } else {
                        Toast.makeText(AtPortDetailActivity.this, rspmessage, Toast.LENGTH_LONG).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    private void getDetail() {
        ProgressDialogUtils.createProgressDialog(AtPortDetailActivity.this, "正在获取数据...");
        HashMap<String, String> map = new HashMap<String, String>();
        Log.e("strSYN", strSYN);
        map.put("strSYN", strSYN);
        httpUtils.get("ZHSW_zgcbDetail", map, "ZHSW_zgcbDetail");
    }

    public static Bitmap base64ToBitmap(String base64Data) {
        byte[] bytes = Base64.decode(base64Data, Base64.DEFAULT);
        return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.picture:
                Intent intent = new Intent(AtPortDetailActivity.this, PictrueActivity.class);
                intent.putExtra("string", bmpString);
                startActivity(intent);
                break;
        }
    }
}
